package cn.sym.basic.controller;

import cn.sym.basic.dto.RegisterDto;
import cn.sym.basic.exception.MyException;
import cn.sym.basic.service.IVerifyCodeService;
import cn.sym.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 获取图片验证码和短信验证码
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;
    /**
     * 所有要获取图片验证码都可以使用
     */
    @GetMapping("/image/{key}")
    public AjaxResult imgCode(@PathVariable("key")String key){
        try {
            String imgCode = verifyCodeService.getImgCode(key);
            return new AjaxResult().setResultObj(imgCode);
        }catch (MyException e){
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }

    /**
     * 发送短信验证码
     * @param registerDto 接收前端传递的参数对象
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult sendSmsCode(@RequestBody RegisterDto registerDto){
        System.out.println(registerDto);
        try {
            verifyCodeService.sendSmsCode(registerDto);
            return AjaxResult.my();
        }catch (MyException e){
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }

}